using UnityEngine;

namespace Game
{
    public static class MathUtil
    {
        public static (int, float) SplitFloat(float f)
        {
            int a = (int)f;
            float b = f - a;
            if (a < 0 || b < 0) return (a - 1, b + 1);
            return (a, b);
        }

        public static Rect GetBoundingRect(Vector2 pos1, Vector2 pos2)
        {
            float x = Mathf.Min(pos1.x, pos2.x);
            float y = Mathf.Min(pos1.y, pos2.y);
            float width = Mathf.Abs(pos1.x - pos2.x);
            float height = Mathf.Abs(pos1.y - pos2.y);
            return new Rect(x, y, width, height);
        }
        
        #region MapRange
        public static int MapRange(int value, int from1, int to1, int from2, int to2)
            => (value - from1) / (to1 - from1) * (to2 - from2) + from2;
        
        public static float MapRange(float value, float from1, float to1, float from2, float to2)
            => (value - from1) / (to1 - from1) * (to2 - from2) + from2;

        public static Vector2 MapRange(Vector2 value, Vector2 from1, Vector2 to1, Vector2 from2, Vector2 to2)
            => new Vector3(
                MapRange(value.x, from1.x, to1.x, from2.x, to2.x),
                MapRange(value.y, from1.y, to1.y, from2.y, to2.y));

        public static Vector3 MapRange(Vector3 value, Vector3 from1, Vector3 to1, Vector3 from2, Vector3 to2)
            => new Vector3(
                MapRange(value.x, from1.x, to1.x, from2.x, to2.x),
                MapRange(value.y, from1.y, to1.y, from2.y, to2.y),
                MapRange(value.z, from1.z, to1.z, from2.z, to2.z));
        #endregion
        
        public static bool Between(float v, float a, float b) => a < b ? a <= v && v <= b : b <= v && v <= b;
        public static bool Between(Vector2 v, Vector2 a, Vector2 b) => Between(v.x, a.x, b.x) && Between(v.y, a.y, b.y);
    }
}